package com.xmy.solution.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.xmy.common.utils.DateUtils;
import com.xmy.common.utils.SecurityUtils;
import com.xmy.solution.domain.Categories;
import com.xmy.solution.domain.dto.solution.*;
import com.xmy.solution.mapper.CategoriesMapper;
import com.xmy.solution.mapper.SolutionCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xmy.solution.mapper.solutionsMapper;
import com.xmy.solution.domain.Solutions;
import com.xmy.solution.service.IsolutionsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 方案管理Service业务层处理
 *
 * @author 夏明毅
 * @date 2024-12-29
 */
@Service
public class solutionsServiceImpl implements IsolutionsService {
    @Autowired
    private solutionsMapper solutionsMapper;
    @Autowired
    private CategoriesMapper categoryMapper;
    @Autowired
    private SolutionCategoryMapper solutionCategoryMapper;

    /**
     * 查询方案管理
     *
     * @param id 方案管理主键
     * @return 方案管理
     */
    @Override
    public SolutionReqDTO selectSolutionsById(Long id) {
        SolutionReqDTO solutionReqDTO = solutionsMapper.selectSolutionsById(id);
        List<Categories> categoriesList = categoryMapper.selectCategoriesBySolutionId(solutionReqDTO.getId());
        List<Long> CategoryID = categoriesList.stream().map(item -> item.getId()).collect(Collectors.toList());
        solutionReqDTO.setCategoryID(CategoryID);

        return solutionReqDTO;
    }

    /**
     * 查询方案管理列表
     *
     * @param solutions 方案管理
     * @return 方案管理
     */
    @Override
    public List<Solutions> selectSolutionsList(Solutions solutions) {
        return solutionsMapper.selectSolutionsList(solutions);
    }

    @Override
    public List<SolutionReqDTO> selectSolutionsVOList(SolutionsPageDTO pageParam) {
        List<SolutionReqDTO> solutionDTOS = solutionsMapper.selectSolutionsVOList(pageParam);
        solutionDTOS.forEach(item -> {
            List<Categories> categoriesList = categoryMapper.selectCategoriesBySolutionId(item.getId());
            List<Long> CategoryID = categoriesList.stream().map(item1 -> item1.getId()).collect(Collectors.toList());
            item.setCategoryID(CategoryID);
        });
        return solutionDTOS;
    }

    /**
     * 新增方案管理
     *
     * @param solutions 方案管理
     * @return 结果
     */
    @Override
//    todo 插入异常未指定
    @Transactional
    public int insertSolutions(SolutionReqDTO solutions) {
//        添加方案信息
        solutions.setCreateBy(SecurityUtils.getUsername());
        solutions.setCreateTime(DateUtils.getNowDate());
        solutions.setSolutionStatus(0l);
        solutionsMapper.insertSolutions(solutions);
        Long id = solutions.getId();
        solutionCategoryMapper.insertDatas(id, solutions.getCategoryID());
        return id.intValue();
    }

    /**
     * 修改方案管理
     *
     * @param solutions 方案管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSolutions(SolutionReqDTO solutions) {

        solutions.setUpdateBy(SecurityUtils.getUsername());
        solutions.setUpdateTime(DateUtils.getNowDate());
        int row = solutionsMapper.updateSolutions(solutions);
        solutionCategoryMapper.deleteSolutionCategoryBySolutionId(solutions.getId());
        solutionCategoryMapper.insertDatas(solutions.getId(), solutions.getCategoryID());
        return row;
    }

    /**
     * 批量删除方案管理
     *
     * @param ids 需要删除的方案管理主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSolutionsByIds(Long[] ids) {
        solutionCategoryMapper.deleteSolutionCategoryBySolutionIds(ids);
        return solutionsMapper.deleteSolutionsByIds(ids);
    }

    /**
     * 删除方案管理信息
     *
     * @param id 方案管理主键
     * @return 结果
     */
    @Override
    public int deleteSolutionsById(Long id) {
        return solutionsMapper.deleteSolutionsById(id);
    }

    /**
     * 查询小程序首页方案信息
     *
     * @param
     * @return
     */
    @Override
    public List<SolutionAppResDTO> selectSolutionAppReqDTOList(SolutionAppReqDTO solutions) {
        List<SolutionAppResDTO> list = solutionsMapper.selectSolutionsAppReqDTO(solutions);

        list.forEach(item -> {
            item.setCategories(categoryMapper.selectCategoriesBySolutionId(item.getId()));
        });
        return list;
    }

    @Override
    public SolutionAppResDTO selectAppSolutionsById(Long id, Long userId) {
        SolutionAppResDTO solutionReqDTO = solutionsMapper.selectSolutionsAppReqDTOById(id, userId);

        List<Categories> categoriesList = categoryMapper.selectCategoriesBySolutionId(solutionReqDTO.getId());
        solutionReqDTO.setCategories(categoriesList);
        return solutionReqDTO;
    }
}
